import { ApiProperty } from "@nestjs/swagger";
import { Exclude } from "class-transformer";
import { Entity, Column, ManyToMany, Relation, JoinTable } from "typeorm";
import { CommonEntity } from "./common.entity";
import { UserEntity } from "./user.entity";
import { PermissionEntity } from "./permission.entity";

@Entity({ name: "role" })
export class RoleEntity extends CommonEntity {
    @ApiProperty({ description: "角色名称" })
    @Column({ unique: true })
    name: string;

    @ApiProperty({ description: "角色描述" })
    @Column()
    description: string;

    @Exclude()
    @ManyToMany((type) => UserEntity, (user) => user.roles, {
        onDelete: "CASCADE",
    })
    users: Relation<UserEntity[]>;

    @ApiProperty({ type: [PermissionEntity], description: "角色权限表" })
    @ManyToMany((type) => PermissionEntity, (per) => per.roles)
    @JoinTable({
        name: "roles_permissions",
        joinColumn: { name: "role_id", referencedColumnName: "id" },
        inverseJoinColumn: { name: "per_id", referencedColumnName: "id" },
    })
    permissions: Relation<PermissionEntity[]>;
}
